<?php
/* @var $this SurveyUserController */
/* @var $model SurveyUser */

$this->breadcrumbs=array(
	Yii::t('base','Manually Draw')=>array('admin'),
	Yii::t('base','Manage'),
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#survey-user-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");

$userId = $_GET['user_id'];
$activity_code = $_GET['activity_code'];
?>
<input type="hidden" name="activity_code" id="activity_code" value="<?php echo $activity_code;?>" />
<input type="hidden" name="user_id" id="user_id" value="<?php echo $userId;?>" />

<script type="text/javascript">
function GetCheckbox(){
	var code = $('#activity_code').val();
	var userId = $('#user_id').val();
    var data=new Array();
    $("input:checkbox[name='suid[]']").each(function (){
        
            if($(this).attr("checked")=="checked"){
                    data.push($(this).val());
            }
    });
    if(data.length > 0){
            $.post("<?php echo Yii::app()->createUrl('surveyUser/confirmPrizeWinning'); ?>",{'suid[]':data,'code':code}, 
            function (data) {
                
                var dataArr = data.split(",");
                
                    if (dataArr[0]=='ok') {
                        alert('<?php echo Yii::t('base', 'Lottery Success!');?>');
  					location.href ="../surveyUser/lottery?user_id="+userId+"&activity_code="+dataArr[1];
                  }
            });
    }else{
            alert('<?php echo Yii::t('base', 'Please select the options you want to draw!');?>');
    }
}
</script>

<h1><?php echo Yii::t('base','Manage');echo Yii::t('base','Manually Draw');?> </h1>

<?php 
	
	$sql="select count(*) as unm from zy_survey_user where partner_id='".$userId."' and winning_status = 1 and activety_code='".$activity_code."'";
	$rs = yii::app()->db->createCommand($sql)->queryAll();
	
	$sqlpeopleunm ="select count(*) as peopleunm from zy_survey_user where partner_id='".$userId."' and winning_status = 0 and activety_code='".$activity_code."'";
	$rss = yii::app()->db->createCommand($sqlpeopleunm)->queryAll();
	
	$activitySql = "select provide_gift_number from zy_activity where activity_code='".$activity_code."'";
	$activityRs = yii::app()->db->createCommand($activitySql)->queryAll();
	
?>
<h2>
<p class="red"><?php echo Yii::t('base', 'The number of prizes:');  echo $activityRs[0]['provide_gift_number'];?>,<?php echo Yii::t('base', 'Currently winning numbers:'); echo $rs[0]['unm'];?>,<?php echo Yii::t('base', 'Subject lottery numbers:'); echo $rss[0]['peopleunm']; ?></p>
</h2>

<?php echo Yii::t('base','<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>');?>

<?php echo CHtml::link(Yii::t('base','Advanced Search'),'#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array('model'=>$model,)); ?>
</div><!-- search-form -->
<?php 
	$level = yii::app()->user->level();
	if($level == 9||$level==1){
?>

<a class="btn btn-small create" title="<?php echo Yii::t('base','Confirm Prize Winning');?>" 
href="javascript:void(0);" onclick="GetCheckbox();">
<?php echo Yii::t('base','Confirm Prize Winning');?></a>

<?php }?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'survey-user-grid',
	'dataProvider'=>$model->search(),
	//'filter'=>$model,
	'itemsCssClass'=>'responsive table table-striped table-bordered',
	'pager'=>array('class'=>'CLinkPager','htmlOptions'=>array('class'=>'pagination pull-right')),
	'pagerCssClass'=>'pagination pull-right',
	'cssFile'=>false,
	'columns'=>array(
		array(
				'id'=>'suid',
				'class'=>'CCheckBoxColumn',
				'selectableRows' => '50',
		),
		array('name'=>'partner_id','value'=>'User::model()->findByPk($data->partner_id)->user_name'),
		//array('name'=>'activety_code','value'=>'GiftPassword::model()->getActivityName($data->activety_code)'),
		'first_name',
		'last_name',
		'email',
		'favo',
		'mobile',
		'Identity_id',
		array('name'=>'award_address_id','value'=>'Activity::model()->getAddressList($data->award_address_id)','visible'=>Yii::app()->user->level() == 9),
		array('name'=>'award_address_id','value'=>'Activity::model()->getAddressLists($data->award_address_id,$data->partner_id)','visible'=>Yii::app()->user->level() == 1),
		array('name'=>'winning_status','value'=>'SurveyUser::model()->switchStatus($data->winning_status)'),
		array('name'=>'sms_status','value'=>'SurveyUser::model()->SmsSwitchStatus($data->sms_status)'),
	),
)); ?>
